﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Abstract;
using log4net;
using DataAccess.Repository;
using DataAccess.NHibernateHelper;
using Domain.Entity;
using Business.Cache;
using Business.Common;

namespace Business.Service
{
    public class CommonServiceImpl : ICommonService
    {
        ILog log = LogManager.GetLogger("CommonServiceImpl");
        TitleRepository titleRepo;
        CountryRepository countryRepo;
        ICacheProvider cache;
        public CommonServiceImpl()
        {
            titleRepo = new TitleRepository(FluentNHibernateHelper.GetContext());
            countryRepo = new CountryRepository(FluentNHibernateHelper.GetContext());
            cache = new CacheProviderImpl();
        }

        public IList<Title> GetAllTitle()
        {
            IList<Title> allTitle = (IList<Title>)cache.Get(Constants.TITLE_CACHE_KEY);
            if (allTitle == null || allTitle.Count == 0)
            {
                allTitle = titleRepo.List().ToList();
                cache.Set(Constants.TITLE_CACHE_KEY,allTitle,1140);
            }

            return allTitle;
        }

        public IList<Country> GetAllCountry()
        {
            IList<Country> allCountry = (IList<Country>)cache.Get(Constants.COUNTRY_CACHE_KEY);
            if (allCountry == null || allCountry.Count == 0)
            {
                allCountry = countryRepo.List().ToList();
                cache.Set(Constants.COUNTRY_CACHE_KEY, allCountry, 1140);
            }

            return allCountry;
        }

       
    }
}
